www.gusucode.com > VC++使用XOR 256算法对文件进行加密解密-源码程序 > VC++使用XOR 256算法对文件进行加密解密-源码程序/code/XOR256Stream/FileCrypt.cpp

    //Download by http://www.NewXing.com

// FileCrypt.cpp : Defines the entry point for the console application.
//

#include "XOR256.h"

#include <iostream>
#include <fstream>

using namespace std;

//Input File Length
streampos FileLength(ifstream& in)
{
	streampos nCurrPos = in.tellg();
	in.seekg(0, ios::end);
	streampos nEndPos = in.tellg();
	//Go Back
	in.seekg(nCurrPos, ios::beg);
	return nEndPos;
}

void ToUpper(string& rostr)
{
	for(int i=0; i<rostr.size(); i++)
		rostr[i] = toupper(rostr[i]);
}

int main(int argc, char* argv[])
{

	try
{
  CStreamXOR256 oStreamXOR256("aaaaaaaa", 9);
  string ostrIn("xxxxxxxx");
  string ostrOut;
  oStreamXOR256.Encrypt(ostrIn, ostrOut);
  oStreamXOR256.Reset();
  ostrIn = "";
  oStreamXOR256.Decrypt(ostrOut, ostrIn);
}
catch(exception& roexception)
{
  cout << roexception.what() << endl;
}

	enum { ENCRYPT=1, DECRYPT=2 };
	char szError[] = "ERROR: Wrong Number of Arguments!";
	if(argc<2)
	{
		//ERROR
		cout << szError << endl;
		return 1;
	}
	//Convert first argument to uppercase
	string ostrFirstArg = string(argv[1]);
	ToUpper(ostrFirstArg);
	if(string("/H")==ostrFirstArg || string("-H")==ostrFirstArg)
	{
		cout << "Usage:" << endl;
		cout << "Encryption: FileCrypt [-|/][k]<key> [-|/][e] <textfile> <encryptedfile>" << endl;
		cout << "Decryption: FileCrypt [-|/][k]<key> [-|/][d] <encryptedfile> <textfile>" << endl;
		return 1;
	}
	else if(argc<5)
	{
		//ERROR
		cout << szError << endl;
		return 1;
	}
	else
	{
		//Determine the Key
		string ostrKey = ostrFirstArg.substr(0, 2);
		ToUpper(ostrKey);
		if("-K" != ostrKey && "/K" != ostrKey)
		{
			//ERROR
			cout << "ERROR: Unknown Flag " << ostrKey << endl;
			return 1;
		}
		ostrKey = ostrFirstArg.substr(2);
		//Determine the Action
		string ostrSecondArg = string(argv[2]);
		ToUpper(ostrSecondArg);
		int iAction = 0;		
		if("-E" == ostrSecondArg || "/E" == ostrSecondArg)
			iAction = ENCRYPT;
		else if("-D" == ostrSecondArg || "/D" == ostrSecondArg)
			iAction = DECRYPT;
		else
		{
			//ERROR
			cout << "ERROR: Unknown Flag " << ostrSecondArg << endl;
			return 1;
		}
		//Open Input File
		ifstream in(argv[3], ios::binary); //default is text mode
		if(!in)
		{
			//ERROR
			cout << "ERROR: Cannot open Input File " << argv[3] << endl;
			return 1;
		}
		//Open Output File
		ofstream out(argv[4], ios::trunc|ios::binary); //default is text mode
		if(!out)
		{
			//ERROR
			cout << "ERROR: Cannot open Output File " << argv[4] << endl;
			return 1;
		}
		//Input File Length
		streampos iFileLength = FileLength(in);
		//Reading file in strings of size 512
		enum { BLOCK_SIZE = 512 };
		//Input string
		string ostrIn;
		ostrIn.resize(BLOCK_SIZE);
		//Output string
		string ostrOut;
		ostrOut.resize(BLOCK_SIZE);
		CStreamXOR256* poStreamXOR256 = new CStreamXOR256(ostrKey, 9);
		for(int i=0; i<iFileLength/BLOCK_SIZE; i++)
		{
			in.read(const_cast<char*>(ostrIn.c_str()), BLOCK_SIZE);
			if(ENCRYPT == iAction)
				poStreamXOR256->Encrypt(ostrIn, ostrOut);
			else //DECRYPT == iAction
				poStreamXOR256->Decrypt(ostrIn, ostrOut);
			//Write Result
			out.write(ostrOut.c_str(), BLOCK_SIZE);
		}
		//Last block
		int iRem = iFileLength%BLOCK_SIZE;
		ostrIn.resize(iRem);
		ostrOut.resize(iRem);
		if(iRem >0)
		{
			in.read(const_cast<char*>(ostrIn.c_str()), iRem);
			if(ENCRYPT == iAction)
				poStreamXOR256->Encrypt(ostrIn, ostrOut);
			else //DECRYPT == iAction
				poStreamXOR256->Decrypt(ostrIn, ostrOut);
			//Write Result
			out.write(ostrOut.c_str(), iRem);
		}
		delete poStreamXOR256;
		//End Application
		in.close();
		out.close();
		if(ENCRYPT == iAction)
			cout << "File " << argv[3] << " Successfully Encrypted!" << endl;
		else //DECRYPT == iAction
			cout << "File " << argv[3] << " Successfully Decrypted!" << endl;
	}
	//OK
	return 0;
}